package com.zhang.service;

import com.zhang.data_object.AlbumDO;
import com.zhang.data_object.TrackDO;
import com.zhang.repository.AlbumRepo;
import com.zhang.repository.RelationRepo;
import com.zhang.repository.TrackRepo;
import com.zhang.view_object.BindVO;
import com.zhang.view_object.TrackVO;
import com.zhang.view_object.UserVO;
import com.zhang.view_object.publish.PlayAlbumView;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author happy
 **/
public class AlbumService {
    private final AlbumRepo albumRepo = new AlbumRepo();
    private final RelationRepo relationRepo = new RelationRepo();
    private final TrackRepo trackRepo = new TrackRepo();

    public Object getBindInfo(UserVO currentUser, int aid) {
        AlbumDO albumDO = albumRepo.selectOneByUidAndAid(currentUser.uid, aid);
        if (albumDO == null) {
            throw new RuntimeException("专辑aid不存在");
        }
        List<Integer> tidList = relationRepo.selectListByAid(aid);
        List<TrackDO> trackDOList;
        if (tidList.isEmpty()) {
            trackDOList = new ArrayList<>();
        } else {
            trackDOList = trackRepo.selectListByTidList(tidList);
        }
        return new BindVO(currentUser, albumDO, trackDOList);
    }

    public PlayAlbumView getAlbumDetail(String aid) {
        PlayAlbumView playAlbumView = albumRepo.selectByAidAndUid(aid);
        List<Integer>  tidList = relationRepo.selectListByAid(Integer.parseInt(aid));
        if (tidList == null) {
            throw new RuntimeException("专辑列表为空");
        }
        playAlbumView.trackList = trackRepo.selectListByTidList(tidList).stream().map(d -> new TrackVO(d.tid,d.title)).collect(Collectors.toList());
        return playAlbumView;
    }
}